package com.example.sharesinfo.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.MyPagerAdapter;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.bean.PersonalCenterBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.fragment.trade.TradingBuyFragment;
import com.example.sharesinfo.fragment.trade.TradingHistoryFragment;
import com.example.sharesinfo.fragment.trade.TradingPositionFragment;
import com.example.sharesinfo.fragment.trade.TradingSellFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class TradingActivity extends BaseUIActivity implements View.OnClickListener {
    private ImageButton ib_back;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private TradingBuyFragment mTradingBuyFragment;
    private TradingSellFragment mTradingSellFragment;
    private TradingPositionFragment mTradingPositionFragment;
    private TradingHistoryFragment mTradingHistoryFragment;
    private int index;
    private int currentTabIndex;
    //上面的切换栏
    private TextView tv_buy;
    private TextView tv_sell;
    private TextView tv_position;
    private TextView tv_history;

    public static void startActivity(Context mContext, int index) {
        Intent intent = new Intent(mContext, TradingActivity.class);
        intent.putExtra("index", index);
        mContext.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trading);
        initView();
        index = getIntent().getIntExtra("index", 0);
        currentTabIndex = index;
        setView(index);
        changeMainFragment(index);

    }

    private void initView() {
        ib_back = findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        tv_buy = findViewById(R.id.tv_buy);
        tv_buy.setOnClickListener(this);
        tv_sell = findViewById(R.id.tv_sell);
        tv_sell.setOnClickListener(this);
        tv_position = findViewById(R.id.tv_position);
        tv_position.setOnClickListener(this);
        tv_history = findViewById(R.id.tv_history);
        tv_history.setOnClickListener(this);

        mTradingBuyFragment = new TradingBuyFragment().getFragment();
        mTradingSellFragment = new TradingSellFragment().getFragment();
        mTradingPositionFragment = new TradingPositionFragment();
        mTradingHistoryFragment = new TradingHistoryFragment();
        mFragmentList.add(mTradingBuyFragment);
        mFragmentList.add(mTradingSellFragment);
        mFragmentList.add(mTradingPositionFragment);
        mFragmentList.add(mTradingHistoryFragment);
    }

    private void changeMainFragment(int index) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        transaction.hide(mFragmentList.get(currentTabIndex));
        if (!mFragmentList.get(index).isAdded()) {
            transaction.add(R.id.main_container, mFragmentList.get(index));
        }
        transaction.show(mFragmentList.get(index)).commit();
    }

    /**
     * 设置导航栏view的样式
     */
    private void setView(int index) {
        switch (index) {
            case 0:
                tv_buy.setTextSize(18);
                tv_buy.setTextColor(Color.WHITE);
                tv_sell.setTextSize(15);
                tv_sell.setTextColor(Color.BLACK);
                tv_position.setTextSize(15);
                tv_position.setTextColor(Color.BLACK);
                tv_history.setTextSize(15);
                tv_history.setTextColor(Color.BLACK);
                break;
            case 1:
                tv_buy.setTextSize(15);
                tv_buy.setTextColor(Color.BLACK);
                tv_sell.setTextSize(18);
                tv_sell.setTextColor(Color.WHITE);
                tv_position.setTextSize(15);
                tv_position.setTextColor(Color.BLACK);
                tv_history.setTextSize(15);
                tv_history.setTextColor(Color.BLACK);
                break;
            case 2:
                tv_buy.setTextSize(15);
                tv_buy.setTextColor(Color.BLACK);
                tv_sell.setTextSize(15);
                tv_sell.setTextColor(Color.BLACK);
                tv_position.setTextSize(18);
                tv_position.setTextColor(Color.WHITE);
                tv_history.setTextSize(15);
                tv_history.setTextColor(Color.BLACK);
                break;
            case 3:
                tv_buy.setTextSize(15);
                tv_buy.setTextColor(Color.BLACK);
                tv_sell.setTextSize(15);
                tv_sell.setTextColor(Color.BLACK);
                tv_position.setTextSize(15);
                tv_position.setTextColor(Color.BLACK);
                tv_history.setTextSize(18);
                tv_history.setTextColor(Color.WHITE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.tv_buy:
                if (index != 0) {
                    setView(0);
                    changeMainFragment(0);
                    index = 0;
                }
                currentTabIndex = 0;
                break;
            case R.id.tv_sell:
                if (index != 1) {
                    setView(1);
                    changeMainFragment(1);
                    index = 1;
                }
                currentTabIndex = 1;
                break;
            case R.id.tv_position:
                if (index != 2) {
                    setView(2);
                    changeMainFragment(2);
                    index = 2;
                }
                currentTabIndex = 2;
                break;
            case R.id.tv_history:
                if (index != 3) {
                    setView(3);
                    changeMainFragment(3);
                    index = 3;
                }
                currentTabIndex = 3;
                break;
        }
    }


}
